import axios from 'axios';
import qs from 'qs'

// export const BASEURL = 'http://120.78.82.3:8001/api';
export const BASEURL = 'http://192.168.2.240:8001/api';

const instance = axios.create({
  baseURL: BASEURL,
  timeout: 30000,
  headers: {
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
});
let pagesize = (sessionStorage.getItem('pagesize')) ? sessionStorage.getItem('pagesize') : 10;
// 请求拦截器 发送请求之前做一些处理
instance.interceptors.request.use(function(config) {
  // 数据的处理
  if (config.method === 'POST' || config.method === 'PUT') {
    config.data = qs.stringify(config.data)
  }
  return config
}, function(error) {
  // 当请求异常时做一些处理
  return Promise.reject(error)
})

// 登录
export function login(dataJson) {
  return instance({
    url: 'login',
    method: 'POST',
    data: dataJson
  })
}

// 设备报警信息
export function deviceAlarmList(page) {
  return instance({
    url: `DeviceAlarmMessage?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 设备配置信息表
export function deviceConfigList(page) {
  return instance({
    url: `DeviceConfig?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 设备配置信息表详情
export function deviceConfigs(deviceId, method, dataJson) {
  let url = '';
  if(deviceId){
    url = `DeviceConfig/${deviceId}`
  }else{
    url = `DeviceConfig`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// IC卡信息表
export function IC(page) {
  return instance({
    url: `IC?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// IC卡信息表详情
export function getCards(icId) {
  return instance({
    url: `IC/${icId}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// IC卡信息表增、删、改
export function cards(icId, methods, dataJson) {
	var url = "";
  if(icId){
		url = `IC/${icId}`
  }else{
  	url = `IC`
  }
  return instance({
    url: url,
    method: methods,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// IC卡信息状态
export function cardStatus() {
  return instance({
    url: `IC/IcStatus`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 制卡记录表
export function icRecord(page) {
  return instance({
    url: `IcRecord?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 内部车辆信息表
export function vehicleList(page, empty_type) {
  let url = '';
  if(empty_type.length > 0){
    url = `InternalVehicle`;
    return instance({
      url: url,
      method: 'GET',
      params : {
        empty_type: empty_type,
      },
      headers: {'Authorization': 'Bearer'+sessionStorage.token},
    })
  }else{
    url = `InternalVehicle?page=${page}&pageSize=${pagesize}`;
    return instance({
      url: url,
      method: 'GET',
      headers: {'Authorization': 'Bearer'+sessionStorage.token},
    })
  }
}

// 内部车辆信息表详情
export function vehicleLists(printCode, method, dataJson) {
  var url = "";
  if(printCode){
    url = `InternalVehicle/${printCode}`
  }else{
    url = `InternalVehicle`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 登记记录表
export function loginHistory(page) {
  return instance({
    url: `LoginHistory?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 结算重量表
export function settlementWeight(page) {
  return instance({
    url: `SettlementWeight?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 结算重量表详情
export function settlementWeightDetail(systemNumber, method, dataJson) {
  var url = '';
  if(systemNumber){
    url = `SettlementWeight/${systemNumber}`;
  }else{
    url = `SettlementWeight`;
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 结算重量是否过磅
export function getIsWeight() {
  return instance({
    url: `SettlementWeight/Weight`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 系统设置表
export function systemConfig(page) {
  var url = '';
  if(page){
    url = `SystemConfig?page=${page}&pageSize=${pagesize}`;
  }else{
    url = `SystemConfig`;
  }
  return instance({
    url: url,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 系统设置表改
export function systemConfigs(id, method, dataJson) {
  var url = "";
  if(id){
    url = `SystemConfig/${id}`;
  }else{
    url = `SystemConfig`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 系统设置表是否试磅
export function isShibang() {
  return instance({
    url: `SystemConfig/ShiBang`,
    method: 'POST',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}


// 用户信息表
export function account(page) {
  return instance({
    url: `Account?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户信息表详情
export function accounts(accountId, method, dataJson) {
  return instance({
    url: `Account/${accountId}`,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 称重异常信息表
export function weighAbnormal(page) {
  return instance({
    url: `WeighAbnormal?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 称重记录表
export function weightData(page, is_finished) {
  var url = '';
  if(is_finished){
    url = `Weightdata?page=${page}&pageSize=${pagesize}&is_finished=N`
  }else{
    url = `Weightdata?page=${page}&pageSize=${pagesize}`
  }
  return instance({
    url: url,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 抓拍异常
export function snapAbnormal(page) {
  return instance({
    url: `SnapAbnormal?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 抓拍异常修改
export function editSnapAbnormal(id, method, dataJson) {
  var url = '';
  if(id){
    url = `SnapAbnormal/${id}`;
  }else{
    url = `SnapAbnormal`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 抓拍异常处理情况
export function getHandlingSituation() {
  return instance({
    url: `SnapAbnormal/HandlingSituation`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 系统异常
export function systemAbnormal(page) {
  return instance({
    url: `SystemAbnormal?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户管理表
export function user(page) {
  return instance({
    url: `User?page=${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户管理表详情
export function users(userId, method, dataJson) {
  var url = "";
  if(userId){
    url = `User/${userId}`;
  }else{
    url = `User`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户角色
export function userRole() {
  return instance({
    url: `User/Role`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 角色管理表
export function role(page) {
  let url = ''
  if(page){
    url = `Role?page=${page}&pageSize=${pagesize}`
  }else{
    url = `Role`
  }
  return instance({
    url: url,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户管理表详情
export function roles(id, method, dataJson) {
  var url = "";
  if(id){
    url = `Role/${id}`;
  }else{
    url = `Role`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 权限管理表
export function permission(page) {
  let url = ''
  if(page){
    url = `Permission?page=${page}&pageSize=${pagesize}`
  }else{
    url = `Permission`
  }
  return instance({
    url: url,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 用户管理表详情
export function permissions(id, method, dataJson) {
  var url = "";
  if(id){
    url = `Permission/${id}`;
  }else{
    url = `Permission`
  }
  return instance({
    url: url,
    method: method,
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 获取改用户的权限
export function getPermissions() {
  return instance({
    url: `permissions`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// FlowExtemal流程记录（外部车辆）
export function flowExtemal(page) {
  return instance({
    url: `flow/FlowExtemal?${page}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// flowExtemalEdit便就修改流程记录（外部车辆）
export function flowExtemalEdit(text, type, para) {
  let url = "";
  if(type == 'purchase_weight'){
    url = `flow/purchase_weight/${text}`;
  }else if(type == 'discharge_weight'){
    url = `flow/discharge_weight/${text}`;
  }else if(type == 'pick_weight'){
    url = `flow/pick_weight/${text}`;
  }else if(type == 'out_weight'){
    url = `flow/out_weight/${text}`;
  }else if(type == 'is_return'){
    url = `flow/is_return/${text}`;
  }else if(type == 'is_go'){
    url = `flow/is_go/${text}`;
  }else if(type == 'remarks'){
    url = `flow/remark/${text}`;
  }
  return instance({
    url: url,
    method: 'PUT',
    data: para,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 是否退货列表
export function isReturn() {
  return instance({
    url: `flow/is_return`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 是否可以出门
export function isGo() {
  return instance({
    url: `flow/is_go`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 内部车辆列表
export function flowInside(page) {
  return instance({
    url: `inside/FlowInside?${page}&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 内部车辆修改内容
export function internalVehicleEdit(text, type, para) {
  let url = "";
  if(type == 'purchase_weight'){
    url = `InternalVehicle/updatePurchaseWeight/${text}`;
  }else if(type == 'discharge_weight'){
    url = `InternalVehicle/updateDischargeWeight/${text}`;
  }else if(type == 'pick_weight'){
    url = `InternalVehicle/updatePickWeight/${text}`;
  }else if(type == 'is_return'){
    url = `InternalVehicle/updateIsReturn/${text}`;
  }else if(type == 'is_go'){
    url = `InternalVehicle/updateIsGo/${text}`;
  }else if(type == 'remarks'){
    url = `InternalVehicle/updateException/${text}`;
  }else if(type == 'is_void'){
    url = `InternalVehicle/updateIsVoid/${text}`;
  }else if(type == 'empty_type'){
    url = `InternalVehicle/updateEmptyType/${text}`;
  }else if(type == 'is_loading'){
    url = `InternalVehicle/updateIsLoading/${text}`;
  }else if(type == 'success'){
    url = `InternalVehicle/success/${text}`;
  }
  return instance({
    url: url,
    method: 'PUT',
    data: para,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 内部流程汇总
export function summary(text, empty_type) {
  let url = '';
  if(empty_type == '0' || empty_type == '1'){
    url = `Summary?${text}&empty_type=${empty_type}`;
  }else{
    url = `Summary/entry_exit_record?${text}`;
  }
  return instance({
    url: url+`&pageSize=${pagesize}`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

// 打印配置
export function printConfig() {
  return instance({
    url: `/printConfig`,
    method: 'GET',
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}

export function printConfigPost(dataJson) {
  return instance({
    url: `/printConfig`,
    method: 'POST',
    data: dataJson,
    headers: {'Authorization': 'Bearer'+sessionStorage.token},
  })
}